// Package database @program: rankinglist
//@author: edte
//@create: 2021-12-29 16:31
package database

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	log2 "rankinglist/log"
	"rankinglist/tools"
)

func QueryPlayerScore(cxt context.Context, listName string, startPage int, endPage int) ([]string, error) {
	redisPath := tools.GetEnv("REDIS_PATH", "localhost:6379")
	redisPD := tools.GetEnv("REDIS_PASSWORD", "")
	rdb := redis.NewClient(&redis.Options{
		Addr:     redisPath,
		Password: redisPD, // no password set
		DB:       4,       // use default DB
		//PoolSize: 500, // 连接池大小
	})
	result, err := rdb.ZRevRange(cxt, listName, int64(startPage), int64(endPage)).Result()
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "QueryPlayerScore", "listname:"+listName, "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return nil, err
	}
	writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "QueryPlayerScore", "listname:"+listName, "Warring", "查询成功")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	return result, err
}
